package Chat;

import java.io.IOException;
import java.io.ObjectInputStream;

import javax.swing.text.BadLocationException;

import VeLaBai.*;


import gui.ServerInfo;


public class ThreadNhanTinHieuChat extends Thread {

	public ServerInfo si;
	public ObjectInputStream ois;
	
	public ThreadNhanTinHieuChat(ServerInfo o) {
		setName("Thread nhan tin hieu chat");
		
		si = o;
		
	}
	
	@Override
	public void run() {
		System.out.println("Start Thread nhan tin hieu chat ...");
		try {
			ois = new ObjectInputStream(si.chatSocket.getInputStream());
		} catch (IOException e) {
			if(si.chatSocket != null && !si.chatSocket.isClosed()) {
				try {
					si.chatSocket.close();
					si.chatSocket = null;
				} catch (IOException e1) {
					//e1.printStackTrace();
				}
			}
			si.threadNhanTHChat = null;

			System.out.println("Thread nhan tin hieu chat da dong !");
			return;
		}
		
		
		String s;
		
		
		while(true) {			
			try {
				s = ois.readUTF();
				System.out.println(s);
				String[] sli = s.split(";");
				if(sli[0].equals("ds")){
					//danh sach la bai
					for(int i=1; i<14; i++){
						DSLaBai.Dslabai.add(sli[i]);
					}
					main frame = new main();
					frame.setVisible(true);
				}else if(sli[0].equals("clientdb")){
				//danh bai
				}else if(sli[0].equals("winner")){
					//danh bai
					}
	
			} catch (IOException e) {
				if(si.chatSocket != null && !si.chatSocket.isClosed()) {
					try {
						si.chatSocket.close();
						si.chatSocket = null;
					} catch (IOException e1) {
						e1.printStackTrace();
					}
				}
				si.threadNhanTHChat = null;

				System.out.println("Thread nhan tin hieu chat da dong !");
				break;
			}
			
		}
	}

}
